<?php
header("Content-Type:text/html;charset=utf-8");
$db=new mysqli("localhost","root","","db_emp","3306");
   if ($db->connect_error)
   {
       die("连接失败，原因".$db->connect_error);
   }
   /*搜索*/
   $pagesize=5;
   $page=(isset($_GET['page']))?$_GET['page']:1;
   $search=(isset($_GET['search']))?$_GET['search']:"";
   $db->query("set names utf8");
   $sql="select * from tb_emp where e_dept like '%{$search}%' or e_name like '%{$search}%'";
   $result=$db->query($sql);
   $tsum=$result->num_rows;
   $maxpage=ceil($tsum/$pagesize);//求出来的最大的页码    
   $page=($page<=0)?1:$page;
   $page=($page>$maxpage)?$maxpage:$page;
   $first=($page-1)*$pagesize;//起始位置


   $sql="select * from tb_emp where e_dept like '%{$search}%' or e_name like '%{$search}%' order by e_id desc limit {$first},{$pagesize}";
   $result=$db->query($sql);
   $sum=$result->num_rows;
   $emplist=$result->fetch_all(MYSQLI_BOTH);
   include "yg.php";

$result->free();
$db->close();